package o2o.web.actions.divide;

import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.divide.CityAgentFeeMemory;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.divide.CityAgentFeeInfo;
import o2o.dao.VO.agent.CityAgentVO;
import o2o.dao.VO.divide.CityAgentFeeVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2019/9/28 10:26
 * @Classname: CityAgentFeeMan
 * @To change this template use File | Settings | File Templates.
 */
public class CityAgentFeeMan {

    // info ---->vo
    public static CityAgentFeeVO convert2Vo(CityAgentFeeInfo info) {
        CityAgentFeeVO vo = new CityAgentFeeVO();
        BeanUtils.copyProperties(info, vo);
        return vo;
    }

    // 后台获取商圈
    public static List<CityAgentFeeVO> listOfAgentIdVO(Long agentId) {
        List<CityAgentFeeInfo> agentFeeInfos = listOfAgentId(agentId);
        if (agentFeeInfos != null && !agentFeeInfos.isEmpty()) {
            return agentFeeInfos.stream().map(a -> convert2Vo(a)).collect(Collectors.toList());
        }
        return null;
    }

    // 获取商圈的
    public static List<CityAgentFeeInfo> listOfAgentId(Long agentId) {
        List<CityAgentFeeInfo> agentFeeInfos = CityAgentFeeMemory.getInstance().listOfAgent(agentId);
        if (agentFeeInfos == null || agentFeeInfos.isEmpty()) {
            return null;
        }
        if (agentFeeInfos != null && agentFeeInfos.size() > 1) {
            Comparator<CityAgentFeeInfo> defaultComparator = Comparator.comparingInt(CityAgentFeeInfo::getIsDefault);
            Comparator<CityAgentFeeInfo> idComparator = Comparator.comparingLong(CityAgentFeeInfo::getId).reversed();
            Collections.sort(agentFeeInfos, defaultComparator.thenComparing(idComparator));
        }
        return agentFeeInfos;
    }

    // 手机端获取
    public static List<CityAgentFeeVO> mobileListOfCityAgentId(Long agentId) {
        List<CityAgentFeeVO> cityAgentFeeVOS = null;
        if (agentId == null) {
            // 获取平台默认的
            listOfAgentIdVO(CityAgentFeeInfo.DELIVERY_DEFAULT_AGENT);
        } else {
            cityAgentFeeVOS = listOfAgentIdVO(agentId);
            if (!ValidateUtil.isAbsList(cityAgentFeeVOS)) {
                cityAgentFeeVOS = listOfAgentIdVO(CityAgentFeeInfo.DELIVERY_DEFAULT_AGENT);
            }
        }
        return cityAgentFeeVOS;
    }

    // 后台服务器计算费用-> 获取指定时间端的费用计算规则
    public static CityAgentFeeInfo getCityAgentFeeByTime(Long agentId, Long time) {
        if (agentId == null) {
            agentId = CityAgentFeeInfo.DELIVERY_DEFAULT_AGENT;
        }
        List<CityAgentFeeInfo> feeInfoList = listOfAgentId(agentId);
        CityAgentFeeInfo info = getCityAgentFee(feeInfoList, time);
        // 上去没有符合规则的 只能去平台的
        if (info == null) {
            feeInfoList = listOfAgentId(CityAgentFeeInfo.DELIVERY_DEFAULT_AGENT);
            info = getCityAgentFee(feeInfoList, time);
        }
        return info;
    }

    // 后台服务器计算费用-> 获取指定时间端的费用计算规则
    public static void assignmentCityAgentFeeByTime(CityAgentVO cityAgent, Long time) {
        if (cityAgent == null) {
            return;
        }
        List<CityAgentFeeInfo> feeInfoList = listOfAgentId(cityAgent.getId());
        CityAgentFeeInfo info = getCityAgentFee(feeInfoList, time);
        if (info != null && (ValidateUtil.isAbsDouble(info.getFixedCost()) || ValidateUtil.isAbsDouble(info.getWithinDistance()) || ValidateUtil.isAbsDouble(info.getOneKmCost()))) {
            cityAgent.setFixedCost(info.getFixedCost());
            cityAgent.setFixedCost(info.getWithinDistance());
            cityAgent.setFixedCost(info.getOneKmCost());
        }
    }

    public static void assignmentCityAgentFeeByTime(CityAgent cityAgent, Long time) {
        if (cityAgent == null) {
            return;
        }
        List<CityAgentFeeInfo> feeInfoList = listOfAgentId(cityAgent.getId());
        CityAgentFeeInfo info = getCityAgentFee(feeInfoList, time);
        if (info != null && (ValidateUtil.isAbsDouble(info.getFixedCost()) || ValidateUtil.isAbsDouble(info.getWithinDistance()) || ValidateUtil.isAbsDouble(info.getOneKmCost()))) {
            cityAgent.setFixedCost(info.getFixedCost());
            cityAgent.setFixedCost(info.getWithinDistance());
            cityAgent.setFixedCost(info.getOneKmCost());
        }
    }

    // 获取当前时间所属于的时间算法
    private static CityAgentFeeInfo getCityAgentFee(List<CityAgentFeeInfo> feeInfoList, Long time) {
        if (feeInfoList != null && !feeInfoList.isEmpty()) {
            for (CityAgentFeeInfo vo : feeInfoList) {
                if (checkValidCityAgentFee(vo, time)) {
                    return vo;
                }
            }
        }
        return null;
    }

    // 检测是否在时间段内
    private static boolean checkValidCityAgentFee(CityAgentFeeInfo info, Long time) {
        if (info == null) {
            return false;
        }
        if (info.getIsOpen() == 0) {
            return false;
        }
        if (info.getIsDefault() == 1) {
            return true;
        }
        if (info.getOneKmCost() == null || info.getWithinDistance() == null || info.getFixedCost() == null) {
            return false;
        }
        String timeContent = info.getTimeContent();
        if (StringUtils.isEmpty(timeContent)) {
            return false;
        }
        String[] split = StringUtils.split(timeContent, "-");
        if (split.length != 2) {
            return false;
        }
        try {
            String start = split[0];
            String end = split[1];
            String[] split1 = StringUtils.split(start, ":");
            if (split1.length != 2) {
                return false;
            }
            Integer start_m = Integer.parseInt(split1[1].toString());
            if (start_m < 10) {
                start = split1[0] + "0" + start_m;
            }
            split1 = StringUtils.split(end, ":");
            if (split1.length != 2) {
                return false;
            }
            Integer end_m = Integer.parseInt(split1[1].toString());
            if (end_m < 10) {
                end = split1[0] + "0" + end_m;
            }
            Integer startTime = Integer.parseInt(start.replaceAll(":", ""));
            Integer endTime = Integer.parseInt(end.replaceAll(":", ""));
            // 判断两个时间
            if (startTime == endTime) {
                // 两个时间相等 表示全天的
                return true;
            }
            // 当前时间
            Integer nowTime = Integer.parseInt(new SimpleDateFormat("HHmm").format(new Date(time)).toString());
            if (endTime == 0) {
                endTime = 2400;
            }
            if (startTime > endTime) {
                // 开始时间大于结束时间 表示跨天的
                if (nowTime <= endTime || nowTime >= startTime) {
                    return true;
                }
            } else {
                if (nowTime >= startTime && nowTime <= endTime) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

}
